<?php
namespace App\Providers\VersionTraits;

use App\Wen\Utils\Settings;
use Dcat\Admin\Models\Menu;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

trait _1_2_10_2_trait{

    function _v1_2_2_1(){
        $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_circle'));
        foreach($table_info_columns as $column){
            if($column->Field == 'tenant_show'){
                $col_type = $column->Type;
                if($col_type == 'tinyint(4)'){
                    Schema::table('wx_circle', function (Blueprint $table) {
                        $table->smallInteger('tenant_show')->default(-1)->change();
                    });
                }
                break;
            }
        }
    }

    function _v1_2_2_2(){
        if(!Schema::hasColumn('wx_user_setting', 'privacy_get_my_wechat')){
            Schema::table('wx_user_setting', function (Blueprint $table) {

                // 1: 关注我的人 2：我关注的人  3：互相关注的人 4：禁止私信 5：向我付费
                $table->decimal('privacy_get_my_phone_pay', 8, 2, true)->after('privacy_follow_me_show')->default(0)->comment('手机号付费数额');
                $table->unsignedTinyInteger('privacy_get_my_phone')->default(4)->after('privacy_follow_me_show')->comment('允许别人获取我的手机号');

                // 1: 关注我的人 2：我关注的人  3：互相关注的人 4：禁止私信 5：向我付费
                $table->decimal('privacy_get_my_wechat_pay', 8, 2, true)->after('privacy_follow_me_show')->default(0)->comment('微信号付费数额');
                $table->unsignedTinyInteger('privacy_get_my_wechat')->default(4)->after('privacy_follow_me_show')->comment('允许别人获取我的微信');

            });
        }
    }

    function _v1_2_2_4(){
        if(!Schema::hasColumn('wx_user_withdrawal', 'withdrawal_remark')){
            Schema::table('wx_user_withdrawal', function (Blueprint $table) {
                $table->string('withdrawal_remark', 150)->after('updated_at')->nullable()->comment('提现说明');
            });
        }
    }

    function _v1_2_2_5(){
        if(!Schema::hasColumn('wx_user_withdrawal', 'withdrawal_scene')){
            Schema::table('wx_user_withdrawal', function (Blueprint $table) {
                $table->unsignedTinyInteger('withdrawal_scene')->after('updated_at')->default(0)->comment('提现场景');
            });
        }


        $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_club_team'));
        foreach($table_info_columns as $column){
            if($column->Field == 'members'){
                $col_type = $column->Type;
                if($col_type == 'varchar(255)'){
                    Schema::table('wx_club_team', function (Blueprint $table) {
                        $table->string('members', 10000)->change();
                    });
                }
                break;
            }
        }

    }


    function _v1_2_2_6(){
        if(!Schema::hasColumn('wx_user', 'invite_benefit')){
            Schema::table('wx_user', function (Blueprint $table) {
                // -1代表默认
                $table->decimal('invite_benefit')->after('who')->default(-1)->comment('邀请收益');
            });
        }

        if(!Schema::hasColumn('wx_club', 'assistants')){
            Schema::table('wx_club', function (Blueprint $table) {
                // -1代表默认
                $table->string('assistants', 2000)->after('is_official')->default('')->comment('店员');
            });
        }

    }

    function _v1_2_2_7(){
        if(!Schema::hasColumn('wx_user_setting', 'teenage_mode_state')){
            Schema::table('wx_user_setting', function (Blueprint $table) {
                $table->string('teenage_mode_password', 4)->nullable()->after('diy_scene_8_post_list_style')->comment('青少年模式密码');
                // 0：默认 1：开启 2：关闭
                $table->unsignedTinyInteger('teenage_mode_state')->default(2)->after('diy_scene_8_post_list_style')->comment('青少年模式开关');
            });
        }
    }


    function _v1_2_2_8(){
        if(!Schema::hasColumn('wx_posts_weight_refresh', 'balance_cost')){
            Schema::table('wx_posts_weight_refresh', function (Blueprint $table) {
                $table->unsignedBigInteger('user_id')->after('id')->nullable()->comment('付费用户');
                $table->integer('coin_cost')->after('refresh_time')->default(0)->comment('金币抵扣');
                $table->decimal('balance_cost')->after('refresh_time')->default(0)->comment('花费余额');
            });
        }
    }

    function _v1_2_2_9(){
        if(!Schema::hasColumn('wx_icons', 'img_width')){
            Schema::table('wx_icons', function (Blueprint $table) {
                $table->unsignedBigInteger('img_height')->after('img')->nullable()->comment('图片高度');
                $table->unsignedBigInteger('img_width')->after('img')->nullable()->comment('图片宽度');
            });
        }
    }

    function _v1_2_2_10(){
        if(!Schema::hasColumn('wx_user', 'invite_benefit_layer')){
            Schema::table('wx_user', function (Blueprint $table) {
                // 1：普通用户 2：推广专员 3：合伙人
                $table->unsignedTinyInteger('invite_benefit_layer')->after('invite_benefit')->default(1)->comment('裂变角色');
            });
        }
    }


    function _v1_2_2_11(){
        if(!Schema::hasTable('wx_posts_comment_manual')){
            Schema::create('wx_posts_comment_manual', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('posts_id')->comment('笔记id');
                $table->unsignedBigInteger('expect_user')->nullable()->comment('期待谁发布');
                $table->string('content')->comment('评论内容');
                $table->string('img')->nullable()->comment('评论图片');
                $table->dateTime('expect_time')->nullable()->comment('期待发表时间');
                $table->dateTime('publish_time')->nullable()->comment('实际发表时间');
                // 0待发表，1：已发表
                $table->unsignedTinyInteger('status')->default(0);
            });
        }
    }

    function _v1_2_2_12(){
        if(!Schema::hasColumn('wx_posts', 'is_anonymous')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_anonymous')->after('is_wechat_sph')->default(0)->comment('是否匿名');
            });
        }
        if(!Schema::hasColumn('wx_user', 'anonymous_name')){
            Schema::table('wx_user', function (Blueprint $table) {
                // 1：普通用户 2：推广专员 3：合伙人
                $table->string('anonymous_name', 100)->after('user_name')->nullable()->comment('匿名名称');
                $table->string('anonymous_avatar')->after('user_avatar')->nullable()->comment('匿名头像');
            });
        }
        if(!Schema::hasColumn('wx_comment', 'is_anonymous')){
            Schema::table('wx_comment', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_anonymous')->after('comment_id')->default(0)->comment('是否匿名');
            });
        }
    }

    function _v1_2_2_13(){
        if(!Menu::where('uri', '/settings/divide')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id =60;
            $admin_menu->order = 24;
            $admin_menu->title = '分成';
            $admin_menu->uri = '/settings/divide';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }
    }


    function _v1_2_2_15(){

        Schema::dropIfExists('wx_user_invite_tmp_code');
        if(!Schema::hasTable('wx_user_invite_temp_code')){
            Schema::create('wx_user_invite_temp_code', function (Blueprint $table) {
                $table->id();
                // 1: 传单  2：桌牌
                $table->string('code')->nullable()->comment('推广码');
                $table->unsignedTinyInteger('type')->default(1)->comment('类型');
                $table->string('path')->default('/pages/tabbar/index/index')->comment('打开页面');
                $table->string('scene')->nullable()->comment('携带参数');
                $table->string('with_target_type')->nullable()->comment('携带动作类型');
                $table->string('with_target_id')->nullable()->comment('携带动作跳转id');
                $table->unsignedBigInteger('user_id')->index('index_0')->nullable()->comment('关联用户');
                $table->unsignedBigInteger('shop_id')->nullable()->comment('关联店铺');
                $table->unsignedBigInteger('club_id')->nullable()->comment('关联俱乐部');
                $table->unsignedBigInteger('circle_id')->nullable()->comment('关联圈子');
                $table->unsignedBigInteger('tag_id')->nullable()->comment('关联话题');
                $table->unsignedBigInteger('voter_id')->nullable()->comment('关联选票');
                $table->unsignedInteger('tenant_id')->nullable()->comment('进入分站');
                // 0待绑定，1：绑定正常 2：解除绑定
                $table->unsignedTinyInteger('status')->default(0);
                $table->timestamps();
            });
        }

        if(!Menu::where('uri', '/invite_benefit/tmp_code')->exists()){
            $invite_benefit_parent_id = Settings::get('menu_invite_benefit_parent_id', 0);
            if($invite_benefit_parent_id > 0){
                $admin_menu = new Menu();
                $admin_menu->parent_id = $invite_benefit_parent_id;
                $admin_menu->order = 2;
                $admin_menu->title = '推广码';
                $admin_menu->uri = '/invite_benefit/tmp_code';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }
        }

        if(!Schema::hasTable('wx_user_invite_temp_code_record')){
            Schema::create('wx_user_invite_temp_code_record', function (Blueprint $table) {
                $table->id();
                // 1: 传单  2：桌牌
                $table->unsignedBigInteger('itc_id')->index('index_0')->comment('推广码');
                $table->unsignedBigInteger('user_id')->index('index_1')->comment('绑定人');
                $table->unsignedBigInteger('object_id')->comment('对象id');
                $table->unsignedDecimal('reward_balance', 10)->default(0)->comment('奖励余额');
                $table->unsignedInteger('reward_coin')->default(0)->comment('奖励金币');
                $table->timestamps();
            });
        }

    }


    function _v1_2_2_16(){
        if(!Schema::hasColumn('wx_tenants', 'city')){
            Schema::table('wx_tenants', function (Blueprint $table) {
                $table->string('city', 50)->nullable()->after('add_city')->comment('城市名称');
            });
        }
    }

    function _v1_2_2_18(){
        // 检查索引是否存在
        $indexExists1 = DB::select(
            DB::raw("SHOW INDEX FROM wx_user_visit WHERE Key_name = 'index_1'")
        );

        if (empty($indexExists1)) {
            Schema::table('wx_user_visit', function (Blueprint $table) {
                $table->index(['type', 'object_id'], 'index_1');
            });
        }

        $indexExists2 = DB::select(
            DB::raw("SHOW INDEX FROM wx_notice WHERE Key_name = 'index_2'")
        );

        if (empty($indexExists2)) {
            Schema::table('wx_notice', function (Blueprint $table) {
                $table->index('is_read', 'index_2');
            });
        }

        $indexExists3 = DB::select(
            DB::raw("SHOW INDEX FROM wx_degree_increment WHERE Key_name = 'index_1'")
        );

        if (empty($indexExists3)) {
            Schema::table('wx_degree_increment', function (Blueprint $table) {
                $table->index('type', 'index_1');
            });
        }

    }

    function _v1_2_2_19(){
        if(!Schema::hasColumn('wx_circle', 'is_city_select')){
            Schema::table('wx_circle', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_tenant_select')->default(1)->after('is_hot')->comment('分站选择');
                $table->unsignedTinyInteger('is_city_select')->default(0)->after('is_hot')->comment('城市选择');
            });
        }
    }

}
